package com.coolwen.service.total;

import com.alibaba.fastjson.JSON;
import com.coolwen.dao.TotalMapper;
import com.coolwen.entity.*;
import com.coolwen.utile.MyComparator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.log4j.Logger;

import java.util.*;

/**
 * Created by nieyu on 2016/8/16.
 */
@Service("totalService")
public class TotalServiceImpl implements TotalService {

    private static final Logger LOGGER = Logger.getLogger(TotalServiceImpl.class);

    @Autowired
    TotalMapper totalMapper;

    @Override
    public int insertSelective(Total total) {
        return totalMapper.insertSelective(total);
    }

    @Override
    public List<Total> queryNewInfo() {
        return totalMapper.queryNewInfo();
    }

    @Override
    public Pager<TotalInfo> find(String grade, String department, String major) {
        int pageSize = SystemContext.getPageSize();
        List<Integer> grades = new ArrayList<Integer>();
//        int pageSize = 100;
        int pageOffset = SystemContext.getPageOffset();

        Pager<TotalInfo> pages = new Pager<TotalInfo>();
        Map<String, Object> params = new HashMap<String, Object>();

        if (grade != null && !grade.equals("")) {

            int g1 = Integer.valueOf(grade);
            int g2 = g1 + 1;
            int g3 = g1 + 2;
            int g4 = g1 + 3;
            grades.add(g1);
            grades.add(g2);
            grades.add(g3);
            grades.add(g4);
            params.put("grade", grades);
        }
        if (department != null && !department.equals("")) {
            params.put("department", "%" + department + "%");
        }
        if (major != null && !major.equals("")) {
            params.put("major", "%" + major + "%");
        }
        params.put("pageSize", pageSize);
        params.put("pageOffset", pageOffset);


        List<TotalInfo> totalInfos = totalMapper.find(params);
        if (grade != null && !grade.equals("")) {

            int g1 = Integer.valueOf(grade);
            int g2 = g1 + 1;
            int g3 = g1 + 2;
            int g4 = g1 + 3;
            grades.add(g1);
            grades.add(g2);
            grades.add(g3);
            grades.add(g4);
            Grade grade1 = new Grade();
            Grade grade2 = new Grade();
            Grade grade3 = new Grade();
            Grade grade4 = new Grade();

            grade1.setGrade(grade);
            grade2.setGrade(String.valueOf(g2));
            grade3.setGrade(String.valueOf(g3));
            grade4.setGrade(String.valueOf(g4));

            for (int i = 0; i < totalInfos.size(); i++) {

                TotalInfo totalInfo = totalInfos.get(i);

                List<String> gs = new ArrayList<String>();
                for (Grade g : totalInfo.getGrades()) {
                    gs.add(g.getGrade());
                }
                if (!gs.contains(grade1.getGrade()))
                    totalInfo.getGrades().add(grade1);
                if (!gs.contains(grade2.getGrade()))
                    totalInfo.getGrades().add(grade2);
                if (!gs.contains(grade3.getGrade()))
                    totalInfo.getGrades().add(grade3);
                if (!gs.contains(grade4.getGrade()))
                    totalInfo.getGrades().add(grade4);

                Collections.sort(totalInfo.getGrades(), new MyComparator());


            }
        }

        int totalRecord = totalMapper.find_count(params);
        LOGGER.info("查找结果11111111" + JSON.toJSON(pages.getDatas()));
        pages.setDatas(totalInfos);
        LOGGER.info("查找结果" + JSON.toJSON(pages.getDatas()));
        pages.setPageOffset(pageOffset);
        pages.setPageSize(pageSize);
        pages.setTotalPage(totalRecord);
        return pages;
    }

    @Override
    public List<TotalInfo> findAll(String grade,String department, String major) {
        Map<String, Object> params = new HashMap<String, Object>();
        List<Integer> grades = new ArrayList<Integer>();
        if (grade != null && !grade.equals("")) {
            int g1 = Integer.valueOf(grade);
            int g2 = g1 + 1;
            int g3 = g1 + 2;
            int g4 = g1 + 3;
            grades.add(g1);
            grades.add(g2);
            grades.add(g3);
            grades.add(g4);
            params.put("grade", grades);
        }
        if (department != null && !department.equals("")) {
            params.put("department", "%" + department + "%");
        }
        if (major != null && !major.equals("")) {
            params.put("major", "%" + major + "%");
        }
        List<TotalInfo> datas = totalMapper.find(params);
        return datas;
    }

}
